﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class User_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindUserSchool();
            General.BindUserProfileList(ddlUserProfile);
            ShowUserDetail(1);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime _dob = new DateTime();
        if (txtDOB.Text.Trim().Length > 0)
        {
            if (!DateTime.TryParse(txtDOB.Text.Trim(), out _dob))
            {
                lblMessage.Text = "DOB is invalid!";
                return;
            }
        }

        if (Page.IsValid)
        {
            MyUser _user = new MyUser();
            MyUserDB _userDB = new MyUserDB();

            _user.UserID = Convert.ToInt32(hidUserID.Value);
            _user.UserName = txtUserName.Text.Trim();
            _user.Password = txtPassword.Text.Trim();
            _user.UserProfileID = Convert.ToInt16(ddlUserProfile.Text);
            _user.PermissionID = Convert.ToInt16(ddlPermission.Text);
            _user.Email = txtEmail.Text;
            _user.PhoneNo = txtPhoneNo.Text;
            _user.IsActive = true;
            _user.CreatedBy = Convert.ToInt32(Session[Global.Session_UserID].ToString());
            _user.ModifiedBy = Convert.ToInt32(Session[Global.Session_UserID].ToString());

            _user = _userDB.Save(_user);

            if (_user.Error.ErrorNo == 0)
            {
                if (_user.UserID > 0)
                {
                    hidUserID.Value = _user.UserID.ToString();

                    UserDetail _userDetail = new UserDetail();
                    UserDetailDB _userDetailDB = new UserDetailDB();
                    _userDetail.UserDetailID = Convert.ToInt32(hidUserDetailID.Value);
                    _userDetail.UserID = _user.UserID;
                    _userDetail.Title = ddlTitle.Text;
                    _userDetail.FirstName = txtFirstName.Text.Trim();
                    _userDetail.LastName = txtLastName.Text.Trim();
                    _userDetail.BirthDate = _dob;
                    _userDetail.Email = txtEmail.Text;
                    _userDetail.PhoneNo = txtPhoneNo.Text;
                    _userDetail.Gender = ddlGender.Text;
                    _userDetail.IsActive = true;
                    _userDetail.CreatedBy = Convert.ToInt32(Session[Global.Session_UserID].ToString());
                    _userDetail.ModifiedBy = Convert.ToInt32(Session[Global.Session_UserID].ToString());

                    _userDetail = _userDetailDB.Save(_userDetail);
                    hidUserDetailID.Value = _userDetail.UserDetailID.ToString();
                    lblMessage.Text = "Saved successfully at " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt");
                    foreach (GridViewRow row in gvSchoolList.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {
                            Label lblUserSchoolID = (Label)row.FindControl("lblUserSchoolID");
                            CheckBox chkSelecSchool = (CheckBox)row.FindControl("chkSelectSchool");
                            Label lblSchoolID = (Label)row.FindControl("lblSchoolID");
                            TextBox txtSchoolStartDate = (TextBox)row.FindControl("txtSchoolStartDate");
                            TextBox txtSchoolDropDate = (TextBox)row.FindControl("txtSchoolDropDate");

                            MyUserSchool _userSchool = new MyUserSchool();
                            MyUserSchoolDB _userSchoolDB = new MyUserSchoolDB();
                            _userSchool.UserSchoolID = Convert.ToInt32(lblUserSchoolID.Text);
                            _userSchool.UserID = _user.UserID;
                            _userSchool.SchoolID = Convert.ToInt32(lblSchoolID.Text);

                            if (chkSelecSchool.Checked)
                            {
                                if (txtSchoolStartDate.Text.Trim().Length > 0)
                                {
                                    DateTime _startDate = new DateTime();

                                    if (DateTime.TryParse(txtSchoolStartDate.Text.Trim(), out _startDate))
                                    {
                                        _userSchool.StartDate = _startDate;
                                    }

                                }

                                if (txtSchoolDropDate.Text.Trim().Length > 0)
                                {
                                    DateTime _dropDate = new DateTime();

                                    if (DateTime.TryParse(txtSchoolDropDate.Text.Trim(), out _dropDate))
                                    {
                                        _userSchool.DropDate = _dropDate;
                                    }

                                }

                                _userSchool = _userSchoolDB.Save(_userSchool);
                            }
                            else
                            {
                                MyError _error = new MyError();
                                _userSchoolDB.Delete(_user.UserID, Convert.ToInt32(lblSchoolID.Text), out _error);
                            }
                        }
                    }
                }
            }
            else
            {
                lblMessage.Text = "Saving failed at " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt") + "<br/>" + _user.Error.ErrorMessage;
            }
        }
    }

    private void ShowUserDetail(int UserID)
    {
        MyUser _user = new MyUser();
        MyUserDB _userDB = new MyUserDB();

        UserDetail _userDetail = new UserDetail();
        UserDetailDB _userDetailDB = new UserDetailDB();

        MyUserSchool _userSchool = new MyUserSchool();
        MyUserSchoolDB _userSchoolDB = new MyUserSchoolDB();

        _user = _userDB.GetItem(UserID);
        if (_user.UserID > 0)
        {
            hidUserID.Value = _user.UserID.ToString();
            txtUserName.Text = _user.UserName;
            txtPassword.Text = _user.Password;
            ddlPermission.Text = _user.PermissionID.ToString();
            ddlUserProfile.Text = _user.UserProfileID.ToString();

            _userDetail = _userDetailDB.GetItem(_user.UserID);
            if (_userDetail.UserDetailID > 0)
            {
                hidUserDetailID.Value = _userDetail.UserDetailID.ToString();
                ddlTitle.Text = _userDetail.Title;
                txtFirstName.Text = _userDetail.FirstName;
                txtLastName.Text = _userDetail.LastName;
                if (_userDetail.BirthDate.Year != 1900)
                {
                    txtDOB.Text = _userDetail.BirthDate.ToString("dd-MMM-yyyy");
                }
                ddlGender.Text = ddlGender.Text;
                txtEmail.Text = _userDetail.Email;
                txtPhoneNo.Text = _userDetail.PhoneNo;

                BindUserSchool();
            }
        }
    }

    private void BindUserSchool()
    {
        MyUserSchoolDB _userSchoolDB = new MyUserSchoolDB();
        SchoolDB _schoolDb = new SchoolDB();
        MyError _error = new MyError();
        DataTable _dtUserSchool = new DataTable();
        DataTable _dtSchool = new DataTable();

        _dtUserSchool = _userSchoolDB.GetUserSchoolList(Convert.ToInt32(hidUserID.Value), out _error);
        _dtSchool = _schoolDb.GetList();

        foreach (DataRow _dr in _dtSchool.Rows)
        {
            DataRow[] _rows = _dtUserSchool.Select("SchoolID=" + _dr["SchoolID"].ToString());
            if (_rows.Length == 0)
            {
                DataRow _userSchool = _dtUserSchool.NewRow();
                _userSchool["UserSchoolID"] = "-1";
                _userSchool["UserID"] = hidUserID.Value;
                _userSchool["SchoolID"] = _dr["SchoolID"].ToString();
                _userSchool["SchoolName"] = _dr["SchoolName"].ToString();
                _dtUserSchool.Rows.Add(_userSchool);
            }
        }

        gvSchoolList.DataSource = _dtUserSchool;
        gvSchoolList.DataBind();
    }

    protected void gvSchoolList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null)
            {
                CheckBox chkIsSelected = (CheckBox)e.Row.FindControl("chkSelectSchool");
                TextBox txtSchoolStartDate = (TextBox)e.Row.FindControl("txtSchoolStartDate");
                TextBox txtSchoolDropDate = (TextBox)e.Row.FindControl("txtSchoolDropDate");
                if (chkIsSelected != null)
                {
                    if (drv["IsSelected"] != null)
                    {
                        if (drv["IsSelected"].ToString().Length > 0 && Convert.ToBoolean(drv["IsSelected"].ToString()) == true)
                        {
                            chkIsSelected.Checked = true;
                        }
                        else
                        {
                            chkIsSelected.Checked = false;
                        }
                    }

                    if (txtSchoolStartDate != null && drv["StartDate"] != null)
                    {
                        DateTime _startDate = new DateTime();
                        if (DateTime.TryParse(drv["StartDate"].ToString(), out _startDate))
                        {
                            if (_startDate.Year != 1900)
                            {
                                txtSchoolStartDate.Text = _startDate.ToString("dd-MMM-yyyy");
                            }
                        }
                    }

                    if (txtSchoolDropDate != null && drv["DropDate"] != null)
                    {
                        DateTime _dropDate = new DateTime();
                        if (DateTime.TryParse(drv["DropDate"].ToString(), out _dropDate))
                        {
                            if (_dropDate.Year != 1900)
                            {
                                txtSchoolDropDate.Text = _dropDate.ToString("dd-MMM-yyyy");
                            }
                        }
                    }
                }
            }
        }
    }
}